package com.zlkj.pro.core.serializer;

import cn.stylefeng.roses.kernel.rule.util.HttpServletUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
import lombok.SneakyThrows;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import static com.zlkj.pro.core.consts.ProjectConstants.DO_NOT_TRANSFER_IDENTITY;
import static com.zlkj.pro.core.consts.ProjectConstants.TRANSFER_FLAG;

/**
 * @author 孙铭泽
 * @date 2020-11-11
 * @time 23:58
 **/
public class VoSerializer extends BeanSerializerModifier {

    @SneakyThrows
    @Override
    public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {
        //获取请求
        //循环所有的beanPropertyWriter
        for (BeanPropertyWriter beanProperty : beanProperties) {
            //判断字段的类型，如果是array，list，set则注册nullSerializer
            JavaType type = beanProperty.getType();
            if (type.isArrayType() || type.isCollectionLikeType()) {
                //给writer注册一个自己的nullSerializer
                beanProperty.assignNullSerializer(new JsonSerializer<Object>() {
                    @Override
                    public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
                        gen.writeStartArray();
                        gen.writeEndArray();
                    }
                });
            } else {
                beanProperty.assignNullSerializer(new JsonSerializer<Object>() {
                    @SneakyThrows
                    @Override
                    public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) {
                        boolean transferFlag = Boolean.FALSE;
                        try {
                            HttpServletRequest request = HttpServletUtil.getRequest();
                            String whetherTheTransfer = request.getHeader(TRANSFER_FLAG);
                            transferFlag = DO_NOT_TRANSFER_IDENTITY.equals(whetherTheTransfer);
                        } catch (Exception ignored) {
                        }
                        if (!type.isJavaLangObject() && !type.isFinal()) {
                            //获取原始对象
                            Class<?> rawClass = type.getRawClass();
                            //如果Date对象是空的时候转成""
                            Class<? extends Date> dateClass = Date.class;
                            if (rawClass.getName().equals(dateClass.getName())) {
                                if (transferFlag) {
                                    gen.writeNull();
                                } else {
                                    gen.writeString("");
                                }
                            } else {
                                gen.writeString("");
                            }
                        } else {
                            if (transferFlag) {
                                gen.writeNull();
                            } else {
                                gen.writeString("");
                            }
                        }
                    }
                });
            }
        }
        return beanProperties;
    }
}
